﻿using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using AMS.DeDuplicator.CommonLayer;

namespace AMS.DeDuplicator.BusinessLayer.Command
{
    public class DeleteFileCommand : Command
    {
        private string _path;

        #region Constructors
        //public DeleteFileCommand()
        //    : this (null)
        //{
        //}

        public DeleteFileCommand(string path)
            : base()
        {
            InputValidator.ValidateNotNull(path);
            InputValidator.ValidateFileExists(path);
            _path = path;
        }
        #endregion

        public override bool Execute()
        {
            if (this.Executed)
                ExceptionHandler.AlreadyExecutedException();

            try
            {
                // move file to recycle bin
                FileSystem.DeleteFile(_path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
            }
            catch (Exception)
            {
                this.Executed = !File.Exists(_path);
                return this.Executed;
            }

            this.Executed = true;
            return true;
        }

        //public override bool Execute(object parameter)
        //{
        //    _path = parameter as string;
        //    InputValidator.ValidateNotNull(_path);
        //    InputValidator.ValidateFileExists(_path);
        //    return this.Execute();
        //}

        public override bool Undo()
        {
            if (!Executed)
                ExceptionHandler.NotExecutedException();

            try
            {
                // attempt to recover from Recycle Bin
                string fileName = _path.Substring(_path.LastIndexOf("\\"));
                string pathInRecyleBin = RecycleBin.GetPath() + fileName;
                File.Move(pathInRecyleBin, _path);
            }
            catch (Exception)
            {
                this.Executed = !File.Exists(_path);
                return !this.Executed;
            }

            this.Executed = false;
            return true;
        }
    }
}
